// 对Date的扩展，将 Date 转化为指定格式的String 
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function(fmt) { // author: meizz
	var o = {
		"M+" : this.getMonth() + 1, // 月份
		"d+" : this.getDate(), // 日
		"h+" : this.getHours(), // 小时
		"m+" : this.getMinutes(), // 分
		"s+" : this.getSeconds(), // 秒
		"q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
		"S" : this.getMilliseconds()
	// 毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
					: (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

/**
 * 全局弹框
 * warn警告，success成功，error错误，info提示
 * 用法1：success(title, content)
 * 用法2：success(content)
 */
var Popup = {
	defaults: {
		warn: {
			title: '警告',
			content: '操作异常！'
		},
		success: {
			title: '成功',
			content: '操作成功！'
		},
		error: {
			title: '错误',
			content: '操作失败！'
		},
		info: {
			title: '消息',
			content: '系统消息！'
		}
	}
};
window.warn = function(title, content){
	if(arguments.length <= 1){
		content = title;
		title = undefined;
	}
	createGrowl({
		title: '<i class="glyphicon glyphicon-question-sign mr5"></i>' + (title || Popup.defaults.warn.title),
		content: content || Popup.defaults.warn.content,
		closeBtn: true,
		persistent: true,
		modal: true
	});
};
window.success = function(title, content){
	if(arguments.length <= 1){
		content = title;
		title = undefined;
	}
	createGrowl({
		title: '<i class="glyphicon glyphicon-ok-sign mr5"></i>' + (title || Popup.defaults.success.title),
		content: content || Popup.defaults.success.content,
		className: 'qtip-green',
		closeBtn: false,
		persistent: false
	});
};
window.error = function(title, content){
	if(arguments.length <= 1){
		content = title;
		title = undefined;
	}
	createGrowl({
		title: '<i class="glyphicon glyphicon-exclamation-sign mr5"></i>' + (title || Popup.defaults.error.title),
		content: content || Popup.defaults.error.content,
		className: 'qtip-red',
		closeBtn: true,
		persistent: true,
		modal: true
	});
};
window.info = function(title, content){
	if(arguments.length <= 1){
		content = title;
		title = undefined;
	}
	createGrowl({
		title: '<i class="glyphicon glyphicon-info-sign mr5"></i>' + (title || Popup.defaults.info.title),
		content: content || Popup.defaults.info.content,
		className: 'qtip-blue',
		closeBtn: false,
		persistent: false
	});
};

window.createGrowl = function(opt) {
	var target = $('.qtip.jgrowl:visible:last');

	$('<div/>').qtip({
		content : {
			text : opt.content,
			title : {
				text : opt.title,
				button : opt.closeBtn
			}
		},
		position : {
			my: 'center', at: 'center',
			target : $(window)
		},
		show : {
			event : false,
			ready : true,
			effect : function() {
				$(this).stop(0, 1).animate({
					opacity : 'toggle'
				}, 400, 'swing');
			},
			delay : 0,
			persistent: opt.persistent,
			modal : {
				on : opt.modal,
				blur : true
			}
		},
		hide : {
			event : false,
			effect : function(api) {
				$(this).stop(0, 1).animate({
					opacity : 'toggle'
				}, 400, 'swing');
			}
		},
		style : {
			width : 280,
			classes : 'jgrowl qtip-shadow ' + opt.className,
			tip : false
		},
		events : {
			render : function(event, api) {
				if (!api.options.show.persistent) {
					$(this).bind('mouseover mouseout', function(e) {
						var lifespan = 2500;

						clearTimeout(api.timer);
						if (e.type !== 'mouseover') {
							api.timer = setTimeout(function() {
								api.hide(e)
							}, lifespan);
						}
					}).triggerHandler('mouseout');
				}
			}
		}
	});
}
window.Loading = $('<div />').qtip({
	content : {
		text : '<div class="ui segment">\
					<div class="ui active dimmer">\
						<div class="ui text mini loader">Loading...</div>\
					</div>\
				</div>'
	},
	position : {
		my : 'center',
		at : 'center',
		target : $(window)
	},
	show : {
		ready : false,
		delay: 0,
		modal : {
			on : true,
			blur : false
		}
	},
	style : 'loading',
	hide: {
		delay: 2000
	}
}).qtip();
window.Confirm = function(title, msg, callback, buttons) {
	var message = $('<p>' + msg + '</p>');
	var btn;
	if(buttons){
		$.each(buttons, function(i, ele){
			var text = ele.text,
				event = ele.event,
				className = ele.className
				btn = $('<button />', {
					"text": text,
					"class": className,
				});
			btn.data('click', event);
			message = message.add(btn);
		});
	}else{
		btn = $('<button />', {
			text: '确定'
		});
		message = message.add(btn)
	}
	
	$('<div />').qtip({
		content : {
			text : message,
			title : title,
			button: true
		},
		position : {
			my : 'center',
			at : 'center',
			target : $(window)
		},
		show : {
			ready : true,
			delay: 0,
			modal : {
				on : true,
				blur : true
			}
		},
		hide : false,
		style : 'dialogue',
		events : {
			render : function(event, api) {
				if(buttons){
					$('button', api.elements.content).click(function(e) {
						api.hide();
						var callback = $(this).data('click');
						if(callback){
							callback();
						}
					});
				}else{
					$('button', api.elements.content).click(function(e) {
						api.hide();
						setTimeout(function(){
							if(callback){
								callback();
							}
						}, 500);
					});
				}
			},
			hide : function(event, api) {
				api.destroy();
			}
		}
	});
};

$.ajaxSetup({
	error: function(xhr, e, text){
		error("Error", text);
	}
});